#### APPENDIX TABLE A1 (DESCRIPTIVES OF EAGER AND RELUCTANT) ####
library(gtsummary)
library(gt)
library(dplyr)

# set working directory
setwd("data/analysis")

# import pooled set
all_data <- read.csv("pooled_data.csv")

# converting variables to factors
all_data$pid7_F <- factor(all_data$PartyID7)
all_data$ATTEND_F <- factor(all_data$ATTEND)
all_data$INCOME_F <- factor(all_data$INCOME)
all_data$EDUC_F <- factor(all_data$EDUC)
all_data$EMPLOY_F <- factor(all_data$EMPLOY)
all_data$HOUSING_F <- factor(all_data$HOUSING)

# drop missing data
all_data <- filter(all_data, !is.na(TNRFU), !is.na(pid7_F), !is.na(ATTEND_F))

# drop duplicates on these covariates
all_data <- all_data |> distinct(TNRFU, GENDER2, EDUC_F, EMPLOY_F, HOME_TYPE5, 
                                 INCOME_F, STATE, MARITAL6, INTERNET, PHONESERVICE5, 
                                 ATTEND_F, METRO, pid7_F, HOUSING_F, HHSIZE, RACE, 
                                 .keep_all = T)

dtab <- all_data |> 
      select(GENDER2, AGE, PID3, EDUC4, RACE, 
             INCOME5, INTERNET2, ATTEND, 
             NRFU) |>
      mutate(EDUC4 = case_match(EDUC4,
                                "1No HS degree" ~ "No HS degree",
                                "2HS degree" ~ "HS degree",
                                "3Some college" ~ "Some college",
                                "4BA or higher" ~ "BA or higher"),
             INCOME5 = case_match(INCOME5,
                                  "[ 1, 7)" ~ "Bottom quintile",
                                  "[ 7,10)" ~ "Second quintile",
                                  "[10,12)" ~ "Third quintile",
                                  "[12,15)" ~ "Fourth quintile",
                                  "[15,18]" ~ "Top quintile"
             ),
             ATTEND = as.numeric(ATTEND)) |>
      tbl_summary(
            by = NRFU,
            statistic = list(all_continuous() ~ c("{mean} ({sd})")),
            type = list(ATTEND ~ "continuous",
                        GENDER2 ~ "dichotomous",
                        INTERNET2 ~ "dichotomous"),
            value = list(GENDER2 ~ "Female",
                         INTERNET2 ~ "2Internet"),
            label = list(GENDER2 ~ "Female", 
                         AGE ~ "Age",
                         EDUC4 ~ "Education",
                         PID3 ~ "Party ID (7-pt)",
                         RACE ~ "Race",
                         INCOME5 ~ "Income",
                         INTERNET2 ~ "Has Internet",
                         ATTEND ~ "Religiosity"),
            sort = list(everything() ~ "frequency"),
            missing = "no"
      ) |> 
      modify_header(label = "Variable") |>
      bold_labels()

# save
dtab |> 
      as_gt() |>
      gtsave("../../results/Table_A1.tex")

